
<template>
  <div>
    <el-row style="margin-top: 20px" type="flex" justify="center">
      <el-col :span="23">
        <el-form
          ref="searchForm"
          :model="searchForm"
          label-width="80px"
          :inline="true"
        >
          <el-form-item label="菜品名称" prop="foodName">
            <el-input v-model="searchForm.foodName"></el-input>
          </el-form-item>
          <el-form-item label="菜品类型" prop="foodType">
            <el-select
              v-model="searchForm.foodType"
              placeholder="请选择菜品类型"
            >
              <el-option
                v-for="item in foodTypes"
                :key="item.id"
                :label="item.foodTypeName"
                :value="item.foodTypeName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-button size="small" type="primary" @click="init">搜索</el-button>
          <el-button size="small" @click="resetSearch('searchForm')"
            >重置</el-button
          >
        </el-form>
      </el-col>
    </el-row>

    <el-row type="flex" justify="center" style="margin-bottom: 10px">
      <el-col :span="23">
        <el-button
          size="small"
          icon="el-icon-plus"
          type="primary"
          @click="addFood"
          >添加菜品</el-button
        >
      </el-col>
    </el-row>

    <el-row type="flex" justify="center">
      <el-col :span="23">
        <el-alert
          title
          type="success"
          :closable="false"
          show-icon
          class="alert_bottom"
        >
          已选择
          <span>{{ 0 }}</span> 项
          <a @click="clearSelectAll" style="color: red">清空</a>
        </el-alert>

        <el-table
          :cell-style="cellstyle"
          size="small"
          ref="multipleTable"
          :border="false"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          height="500"
          :highlight-current-row="true"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="foodName" label="菜品名称"></el-table-column>
          <el-table-column prop="foodImage" label="菜品图片">
            <template slot-scope="scope">
              <img
                style="width: 70px; height: 60px"
                :src="scope.row.foodImage"
              />
            </template>
          </el-table-column>
          <el-table-column
            prop="foodType"
            label="菜品类型"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="foodPrice"
            label="菜品单价"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="foodDesc"
            label="菜品描述"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="isEmpty"
            label="是否售罄"
            show-overflow-tooltip
            :formatter="isEmptyFormatter"
          ></el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-button @click="editFood(scope.row)" type="primary" size="mini"
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                @click="deleteFood(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <el-row type="flex" justify="end">
      <el-col :span="8">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchForm.pageNumber"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="searchForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-col>
    </el-row>

    <el-dialog
      top="6vh"
      :title="titleText"
      :visible.sync="dialogVisible"
      width="32%"
      :before-close="closeDialog"
    >
      <el-row type="flex" justify="center">
        <el-col :span="22">
          <el-form ref="form" :model="form" label-width="80px" :rules="rules">
            <el-form-item label="菜品名称" prop="foodName">
              <el-input v-model="form.foodName" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item label="菜品类型" prop="foodType">
              <el-select
                style="width: 300px"
                v-model="form.foodType"
                placeholder="请选择菜品类型"
              >
                <el-option
                  v-for="item in foodTypes"
                  :key="item.id"
                  :label="item.foodTypeName"
                  :value="item.foodTypeName"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="菜品图片" prop="foodImage">
              <el-input
                v-model="form.foodImage"
                style="width: 300px"
              ></el-input>
            </el-form-item>
            <el-form-item label="是否售罄" style="width: 300px" prop="isEmpty">
              <el-radio-group v-model="form.isEmpty">
                <el-radio :label="'0'">未售罄</el-radio>
                <el-radio :label="'1'">售罄</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="菜品单价" prop="foodPrice">
              <el-input
                v-model="form.foodPrice"
                style="width: 300px"
              ></el-input>
            </el-form-item>

            <el-form-item label="菜品描述" prop="foodDesc">
              <el-input type="textarea" v-model="form.foodDesc"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row type="flex" justify="end" style="margin-top: 20px">
        <el-col :span="7">
          <el-button size="small" @click="closeDialog">取消</el-button>
          <el-button size="small" @click="submit('form')" type="primary"
            >提交</el-button
          >
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  findFoodInfoByCondition,
  submitFood,
  submitEditFood,
  deleteFoodById,
} from "@/api/food/food-api";

import { findTypes } from "@/api/food/food-type-api";
export default {
  /* eslint-disable */
  name: "user",
  data() {
    return {
      //edit / add
      rules: {
        foodName: [
          { required: true, message: "请输入菜品名称", trigger: "blur" },
        ],
        foodType: [
          { required: true, message: "请选择菜品类型", trigger: "change" },
        ],
        isEmpty: [
          { required: true, message: "请选择是否售罄", trigger: "change" },
        ],
        foodPrice: [
          { required: true, message: "请输入菜品价格", trigger: "blur" },
        ],
      },

      titleText: "编辑",
      dialogVisible: false,
      foodTypes: [],

      options: [
        {
          value: 1,
          label: "admin",
        },
        {
          value: 2,
          label: "editor",
        },
      ],
      value1: "",

      // page
      total: 0,
      searchForm: {
        foodType: "",
        username: "",
        phone: "",
        pageNumber: 0,
        pageSize: 10,
      },
      form: {
        name: "",
        phone: "",
        roleIds: [],
      },
      tableData: [],
    };
  },

  methods: {
    isEmptyFormatter(row, column) {
      if (row.isEmpty == 0) {
        return "未售罄";
      } else {
        return "售罄";
      }
    },

    cellstyle(row, column, rowIndex, columnIndex) {
      let cellStyle;
      switch (row.row.isEmpty) {
        case "0":
          cellStyle = "color: green";
          break;
        case "1":
          cellStyle = "color: red";
          break;
        default:
          cellStyle = "color: green";
      }
      if (row.column.label == "是否售罄") {
        return cellStyle;
      }
    },
    addFood() {
      this.titleText = "新增";
      this.form = {};
      this.form.isEmpty = "0";
      this.dialogVisible = true;
    },
    deleteUsers() {},
    clearSelectAll() {},
    handleSelectionChange() {},
    editFood(row) {
      this.titleText = "编辑";
      this.form = row;
      this.dialogVisible = true;
    },
    deleteFood(row) {
      deleteFoodById(row.id).then((response) => {
        if (response.msg === "success") {
          this.$message({ message: "提交成功", type: "success" });
          this.init();
        }
      });
    },
    init() {
      findTypes().then((response) => {
        if (response.msg === "success") {
          console.log(response.data);
          this.foodTypes = response.data;
        }
      });
      findFoodInfoByCondition(this.searchForm).then((response) => {
        if (response.msg === "success") {
          this.total = response.data.totalElements;
          this.tableData = response.data.content;
        }
      });
    },

    //重置
    resetSearch(formName) {
      this.$refs[formName].resetFields();
      this.init();
    },
    //page
    handleSizeChange(pageSize) {
      this.searchForm.pageSize = pageSize;
      this.init();
    },
    handleCurrentChange(pageNumber) {
      this.searchForm.pageNumber = pageNumber;
      this.init();
    },

    closeDialog() {
      this.dialogVisible = false;
    },

    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.titleText === "新增") {
            submitFood(this.form).then((response) => {
              if (response.msg === "success") {
                this.$message({ message: "提交成功", type: "success" });
                this.closeDialog();
                this.init();
              }
            });
          } else {
            submitEditFood(this.form).then((response) => {
              if (response.msg === "success") {
                this.$message({ message: "提交成功", type: "success" });
                this.closeDialog();
                this.init();
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },

  created() {
    this.init();
  },
  mounted() {},
};
</script>

<style scoped>
/* eslint-disable */
.alert_bottom {
  margin-bottom: 20px;
}
/* eslint-disable */
</style>